package com.banmao.CarlCamp.Day09;

import com.alibaba.fastjson.JSON;

/**
 * https://leetcode.cn/problems/remove-element/
 */
public class LC27 {

    public int removeElement(int[] nums, int val) {
        if (nums.length == 0) {
            return 0;
        }

        // 双指针
        int slow = 0, fast = 0;
        while (fast < nums.length) {
            nums[slow] = nums[fast];
            // 当 slow 指向的数值不为目标值，两个指针一起移动
            if (nums[slow] != val) {
                slow++;
                fast++;
            } else {
                // 慢指针指向的值是目标值，慢指针停下，快指针移动
                fast++;
            }
        }

        return slow;
    }

    public static void main(String[] args) {
        LC27 lc27 = new LC27();
        int[] nums = {3, 2, 2, 3};
        int res = lc27.removeElement(nums, 3);

        System.out.println(JSON.toJSONString(nums));
        System.out.println(res);
    }

}
